﻿package 
{
    import builder.MazeBuilder;
    import flash.display.MovieClip;
    import flash.events.KeyboardEvent;

    public class Game
    {
        private var m_container:MovieClip;
        private var m_player:Player;
        private var m_maze:Maze;
        private var m_controller:Controller;

        public function Game(container:MovieClip)
        {
            m_container = container;
            m_player = new Player();
            m_container.stage.addEventListener(KeyboardEvent.KEY_DOWN, function(e:KeyboardEvent):void{m_player.onKeyDown(e)});
        }

        public function createMaze(mazeBuilder:MazeBuilder):void
        {
            mazeBuilder.buildMaze();
            mazeBuilder.buildRoom(1);
            mazeBuilder.buildRoom(2);
            mazeBuilder.buildRoom(3);
            mazeBuilder.buildRoom(4);
            mazeBuilder.buildRoom(5);    
            mazeBuilder.buildDoor(1, 2, GameConstants.NORTH, GameConstants.SOUTH);
            mazeBuilder.buildDoor(1, 3, GameConstants.SOUTH, GameConstants.NORTH);
            mazeBuilder.buildDoor(1, 4, GameConstants.EAST, GameConstants.WEST);
            mazeBuilder.buildDoor(3, 5, GameConstants.WEST, GameConstants.EAST);
            m_maze = mazeBuilder.GetMaze();
            
            initGame();
        }

        private function initGame():void
        {
            m_maze.showRoom(1);
            m_container.addChild(m_maze);
            m_container.addChild(m_player.getPlayer());
            m_controller = new Controller(m_player, m_maze);
        }
    }
}
